/***********************************************************************************
 @file   
 @author
 @attention
 ***********************************************************************************/
#include "mid_rtc.h"
/***********************************************************************************
 * @param
***********************************************************************************/
uint8_t rtc_xsec = 15;

/***********************************************************************************
 * @brief  
***********************************************************************************/
void rtc_config_init(uint8_t sec)
{
    
    if(sec==30)
    {
        rtc_xsec = 15;
    }
    else
    {
        rtc_xsec = 3;
    }
    stc_rtc_initstruct_t RtcInitStruct;
    Sysctrl_SetPeripheralGate(SysctrlPeripheralRtc,TRUE);   //RTC模块时钟打开
    RtcInitStruct.rtcAmpm = RtcPm;                          //12小时制
    RtcInitStruct.rtcClksrc = RtcClkXtl;                    //外部低速时钟
    RtcInitStruct.rtcPrdsel.rtcPrdsel = RtcPrdx;            //周期中断类型PRDX
    RtcInitStruct.rtcPrdsel.rtcPrdx = sec;                  //周期中断时间间隔 9=5秒  27    
    
//    RtcInitStruct.rtcPrdsel.rtcPrdsel = RtcPrds;  //周期中断类型PRDS //秒中断
//    RtcInitStruct.rtcPrdsel.rtcPrds = Rtc1S;      //周期中断事件间隔
    
    RtcInitStruct.rtcTime.u8Second = 0x55;
    RtcInitStruct.rtcTime.u8Minute = 0x01;
    RtcInitStruct.rtcTime.u8Hour   = 0x10;
    RtcInitStruct.rtcTime.u8Day    = 0x17;
    RtcInitStruct.rtcTime.u8DayOfWeek = 0x04;
    RtcInitStruct.rtcTime.u8Month  = 0x04;
    RtcInitStruct.rtcTime.u8Year   = 0x19;
    RtcInitStruct.rtcCompen = RtcCompenEnable;
    RtcInitStruct.rtcCompValue = 0;                         //补偿值 根据实际情况进行补偿
    Rtc_Init(&RtcInitStruct);
    Rtc_AlmIeCmd(TRUE);                                     //使能闹钟中断
    EnableNvic(RTC_IRQn, IrqLevel3, TRUE);                  //使能RTC中断向量
    Rtc_Cmd(TRUE);                                          //使能RTC开始计数
    Rtc_StartWait();                                        //启动RTC计数，如果要立即切换到低功耗，需要执行此函数    
}
/***********************************************************************************
 * @brief  RTC中断入口函数
***********************************************************************************/
void Rtc_IRQHandler(void)
{
    if(Rtc_GetPridItStatus() == TRUE)
    {
        Rtc_ClearPrdfItStatus();             //清除中断标志位     
        printf("device wakeup rtc \r\n");    
        speak_timer();
        lora_config_timer();
        system_adc_attenuation_timer(1);
        
        sleep_device_operation_time(RTC_WAKEUP_TIME);
        sleep_sensor_operation_time(RTC_WAKEUP_TIME);
    }
}